<template>
  <div>
    <!-- 渲染角色列表 -->
    <!-- 多选框 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in clickedRoleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submitFn">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'AssignRole',
  props: {
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    },
    clickedUserInfo: {
      type: Object,
      default: null
    },
    // 角色列表,都有什么角色可供选择
    clickedRoleList: {
      type: Array,
      default: null
    }
  },
  data() {
    return {

      // 多选框选中的值
      checkList: [],

      // 用户信息
      userInfo: {}
    }
  },
  watch: {
    clickedUserInfo() {
      this.userInfo = this.clickedUserInfo
      //   当前员工已经扮演了什么角色
      this.checkList = this.clickedUserInfo.roleIds
    }

  },
  created() {
    // console.log('这里是clickedRoleList')
    // console.log(this.clickedRoleList)
    // this.checkList = this.clickedRoleList
  },
  methods: {
    // 取消弹框
    cancleDialog() {
      // 关闭弹框
      this.$emit('update:close', false)
      //   清空多选框
      this.checkList = []
    },

    // 确定按钮
    submitFn() {
      // 打包数据发送给父组件
    // 整理数据包格式
      const data = {
        id: this.userInfo.id,
        roleIds: this.checkList
      }
      this.$emit('checkList', data)
      //   清空多选框
      this.checkList = []
      // 关闭弹框
      this.$emit('update:close', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
